﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using System.Threading;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Tests.Integration.Instruments
{
    public class CommandExecutor
    {
        private Guid menuGroupGuid;

        private int commandId;


        public ICollection<UIControlAction> UIControlActions { get; private set; }


        public CommandExecutor(Guid menuGroupGuid, int commandId)
        {
            this.menuGroupGuid = menuGroupGuid;
            this.commandId = commandId;
            UIControlActions = new List<UIControlAction>();
        }

        public CommandExecutor(string menuGroupGuid, int commandId)
            : this(new Guid(menuGroupGuid), commandId)
        {
        }


        public void Execute()
        {
            Thread thread = null;
            thread = new Thread(() =>
            {
                foreach (var action in UIControlActions)
                {
                    action.PerformAction();
                }
            });
            thread.Start();

            var guid = menuGroupGuid.ToString("B").ToUpperInvariant();
            object Customin = null;
            object Customout = null;
            VsIdeTestHostContext.Dte.Commands.Raise(guid, commandId, ref Customin, ref Customout);

            if (thread != null)
            {
                thread.Join();
            }
        }
    }
}
